package com.amkai.aes.webservice.mobile.objecthandler;

import java.util.List;

import com.amkai.aes.webservice.mobile.MobileJobObjectHandler;
import com.amkai.common.extensions.hibernate.config.SiteManager;
import com.amkai.common.extensions.hibernate.config.manager.SessionManager;
import com.amkai.mobile.bean.pmcase.MobileCase;
import com.amkai.mobile.bean.registration.MobilePatient;
import com.amkai.mobile.bean.scheduler.appointment.MobileAppointmentBrief;
import com.amkai.mobile.bean.scheduler.appointment.MobileAppointmentDetails;
import com.amkai.mobile.common.job.scheduler.MobileSchedulerJob;
import com.amkai.mobile.common.job.scheduler.MobileSchedulerJobResult;

/**
 * 
 * @author lkovari
 *
 */
public class MobileSchedulerJobHandler implements MobileJobObjectHandler<MobileSchedulerJob, MobileSchedulerJobResult> {
	SessionManager sessionManager = null;

	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	private SessionManager getSessionManager() throws Exception {
		if (sessionManager == null)
			sessionManager = SiteManager.Start();
		return sessionManager;
	}
	
	@Override
	public MobileSchedulerJobResult process(MobileSchedulerJob job) throws Exception {
		MobileSchedulerJobResult mobileSchedulerJobResult = new MobileSchedulerJobResult();
		try {
			switch(job.getMobileSchedulerJobKind()) {
			case DOWNLOAD_CASE_DETAILS_BY_ID : {
				MobileSchedulerServerManager mssm = new MobileSchedulerServerManager(getSessionManager());
				MobileCase mobileCase = mssm.downloadCaseDetailsById(job);
				mobileSchedulerJobResult.setMobileCase(mobileCase);
				break;
			}
			case DOWNLOAD_PATIENT_DETAILS_BY_ID : {
				MobileSchedulerServerManager mssm = new MobileSchedulerServerManager(getSessionManager());
				MobilePatient mobilePatient = mssm.downloadPatientDetailsById(job);
				mobileSchedulerJobResult.setMobilePatient(mobilePatient);
				break;
			}
			case DOWNLOAD_APPT_KIND : {
				MobileSchedulerServerManager mssm = new MobileSchedulerServerManager(getSessionManager());
				boolean isSCorSHAppointment = mssm.isSCorSHAppointment(job);
				mobileSchedulerJobResult.setSCorSHAppointment(isSCorSHAppointment);
				break;
			}
			case DOWNLOAD_NEAREST_APPOINTMENT_BRIEF : {
				MobileSchedulerServerManager mssm = new MobileSchedulerServerManager(getSessionManager());
				MobileAppointmentBrief mobileAppointmentBrief = mssm.downloadNearestAppointment(job);
				mobileSchedulerJobResult.setNearestMobileAppointment(mobileAppointmentBrief);
				break;
			}
			case DOWNLOAD_APPOINTMENT_DETAILS_BY_ID : {
				MobileSchedulerServerManager mssm = new MobileSchedulerServerManager(getSessionManager());
				MobileAppointmentDetails mobileAppointmentDetails = mssm.downloadAppointmentDetailsById(job);
				mobileSchedulerJobResult.setMobileAppointmentDetails(mobileAppointmentDetails);
				break;
			}
			case DOWNLOAD_APPOINTMENTS_AS_BRIEF_BY_TIMERANGE_AND_SURGEON : {
				MobileSchedulerServerManager mssm = new MobileSchedulerServerManager(getSessionManager());
				List<MobileAppointmentBrief> mobileAppointmentsBrief = mssm.downloadAppointmentsBrief(job);
				mobileSchedulerJobResult.setMobileAppointmentsBrief(mobileAppointmentsBrief);
				break;
			}
			}
		}
		catch (Exception e) {
			mobileSchedulerJobResult.setException(e);
		}
		return mobileSchedulerJobResult;
	}

}
